﻿using UnityEngine;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {
	
	public List<GameObject> inventory;
	public int inventorySize = 0;
	
	// Use this for initialization
	void Start () {
		inventory = new List<GameObject>();
	}
	
	public void DropItem(GameObject item) {
		//Drop item
		if (inventory.Remove(item)) {
			item.SetActive(true);
			//position of drop = this.position + camera.direction
			
			item.transform.position = this.gameObject.transform.position + Camera.main.transform.forward * 3;
			item.transform.rotation = Quaternion.identity;
		}
	}
	
	public bool UseItem(GameObject item) {
		UsableInventoryObject uio = (UsableInventoryObject)item.GetComponent("UsableInventoryObject");
		if (uio != null && uio.Use(this.gameObject)) {	
			this.inventory.Remove(item);
			Destroy(item);
			return true;
		}
		return false;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown(KeyCode.E)) {
			Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(r, out hit, 10.0f)) {
				GameObject go = hit.collider.gameObject;
				if (go.GetComponent("InventoryObject") && inventory.Count < inventorySize) {
					print("Hit!");
					go.SetActive(false);
					this.inventory.Add(go);
				} else {
					print("Not an inventory object || inv full!");
				}
			} else {
				print("Miss!");
			}
		}
		if (Input.GetKeyDown(KeyCode.I)) {
			InventoryDisplay mb = (InventoryDisplay)this.GetComponent("InventoryDisplay");
			if (mb != null) {
				mb.enabled = true;
				//Pause game & disable player mouselook + camera up/down movement
				Time.timeScale = 0;
				
				foreach (MouseLook ml in this.GetComponentsInChildren<MouseLook>())
					ml.enabled = false;
			}
		}
		if (Input.GetKeyDown(KeyCode.O)) {
			this.inventorySize += 1;
		}
	}
	
	void OnGUI () {
		//Todo: draw Label if over object
	}
}
